/**
 * File:   PingNodeAction.java
 * Author: Gvennet
 */
package gui.actions;

import dht.network.Network;
import dht.node.DHTNodeModel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.InetAddress;

/**
 * todo write javadoc
 */
public final class PingNodeAction extends AbstractAction
{

	private final DHTNodeModel nodeModel;

	private final JTable nodeTable;

	private final Network network;

	public PingNodeAction(DHTNodeModel nodeModel, JTable nodeTable, Network network)
	{
		super("Проверить доступность");
		putValue(SHORT_DESCRIPTION, "Проверить доступность узла");
		putValue(MNEMONIC_KEY, KeyEvent.VK_P);
		this.nodeModel = nodeModel;
		this.nodeTable = nodeTable;
		this.network = network;
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{

		int index = nodeTable.getSelectedRow();
		if (index >= 0)
		{
			InetAddress ia = nodeModel.getInetAddress(index);
			int port = nodeModel.getPort(index);

			try
			{
				network.sendPingRequest(ia, port);
			}
			catch (IOException e1)
			{
				//do nothing
			}
		}
	}
}
